package enums;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;

/**
 * 枚举式单例
 *
 * @author wly
 * @date 2018/9/7 11:49
 */
public enum ESClientEnum {
    ESCLIENT();

    ESClientEnum() {
        /**
         * 集群名称
         */
        String clusterName = "elasticsearch";
        /**
         * IP
         */
        String ip = "127.0.0.1";
        /**
         * PORT
         */
        int tcpPort = 9300;
        Settings settings = Settings.builder().put("cluster.name", clusterName).build();
        try {
            TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName(ip), tcpPort));
            this.transportClient = client;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private TransportClient transportClient;

    public TransportClient getESClient() {
        return this.transportClient;
    }
}
